/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

/**
 *
 * @author Sasha Rakic
 */

import java.sql.*;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpSession;
public class Neradni {
    private String dan1;
    private String mesec1;
    private String godina1;

    private String dan2;
    private String mesec2;
    private String godina2;

    private String razlog;
    private String sala="laboratorija 25";

    private ArrayList<SelectItem> daniItems = new ArrayList<SelectItem>();
    {for (int i=1;i<32;i++) getDaniItems().add(new SelectItem(i));}

    private ArrayList<SelectItem> meseciItems = new ArrayList<SelectItem>();
    {for (int i=1;i<13;i++) getMeseciItems().add(new SelectItem(i));}

    private ArrayList<SelectItem> godineItems = new ArrayList<SelectItem>();
    {for (int i=2008;i<2020;i++) getGodineItems().add(new SelectItem(i));}

    public void add1(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;

        Timestamp now = new Timestamp(Calendar.getInstance().getTimeInMillis());

        if(Date.valueOf(godina1+"-"+mesec1+"-"+dan1).before(now)){
            context.addMessage(null, new FacesMessage("Тај датум је већ прошао"));
            return;
        }

        try {
            con = DB.getConnection();
            stmt = con.createStatement();

            Date date1 = Date.valueOf(godina1+"-"+mesec1+"-"+dan1);

            String user = ((User)session.getAttribute("user")).getUsername();
            rs = stmt.executeQuery("select kid from korisnik where username='"+user+"';");
            int kid=0;
            if(rs.next())kid=rs.getInt("kid");

            rs = stmt.executeQuery("select sid from sala where naziv='"+sala+"';");
            int sid=0;
            if(rs.next())sid=rs.getInt("sid");

            rs = stmt.executeQuery("select * from neradni_dani where sid='"+sid+"' and datum='"+date1+"';");
            if(rs.next()){
                stmt.executeUpdate("update neradni_dani set razlog='"+razlog+"' where sid='"+sid+"'and datum='"+date1+"';");
                context.addMessage(null, new FacesMessage("Разлог нерадног дана промењен"));
                stmt.close();con.close();
                return;
            }

            String upit = "INSERT INTO neradni_dani (sid, datum, vreme_evidentiranja, korisnik, razlog) VALUES "
                           +"('"+sid+"', '"+date1+"', '"+now+"', '"+kid+"', '"+razlog+"')";
            stmt.executeUpdate(upit);

            context.addMessage(null, new FacesMessage("Нерадни дан успешно додат"));
            stmt.close();con.close();
            return;
        }catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ex) {}
            }
            context.addMessage(null, new FacesMessage(e.getLocalizedMessage()));
            return;
        }
    }

    public void addn(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Connection con = null;
        ResultSet rs = null;
        Statement stmt = null;

        Timestamp now = new Timestamp(Calendar.getInstance().getTimeInMillis());

        if(Date.valueOf(godina1+"-"+mesec1+"-"+dan1).before(now)){
            context.addMessage(null, new FacesMessage("Тај датум је већ прошао"));
            return;
        }

        if(Date.valueOf(godina1+"-"+mesec1+"-"+dan1).compareTo(Date.valueOf(godina2+"-"+mesec2+"-"+dan2))>=0){
            context.addMessage(null, new FacesMessage("Први датум је пре или исти као други"));
            return;
        }

        try {
            con = DB.getConnection();
            stmt = con.createStatement();

            String user = ((User)session.getAttribute("user")).getUsername();
            rs = stmt.executeQuery("select kid from korisnik where username='"+user+"';");
            int kid=0;
            if(rs.next())kid=rs.getInt("kid");

            rs = stmt.executeQuery("select sid from sala where naziv='"+sala+"';");
            int sid=0;
            if(rs.next())sid=rs.getInt("sid");

            Date date1 = Date.valueOf(godina1+"-"+mesec1+"-"+dan1);
            Date date2 = Date.valueOf(godina2+"-"+mesec2+"-"+dan2);

            while(date1.before(date2)){
                rs = stmt.executeQuery("select * from neradni_dani where sid='"+sid+"' and datum='"+date1+"';");
                if(rs.next()){
                    stmt.executeUpdate("update neradni_dani set razlog='"+razlog+"' where sid='"+sid+"'and datum='"+date1+"';");
                }
                else{
                    String upit = "INSERT INTO neradni_dani (sid, datum, vreme_evidentiranja, korisnik, razlog) VALUES "
                           +"('"+sid+"', '"+date1+"', '"+now+"', '"+kid+"', '"+razlog+"')";
                    stmt.executeUpdate(upit);
                }
                long date1mills = date1.getTime();
                date1mills+=24*60*60*1000;
                date1.setTime(date1mills);
            }

            context.addMessage(null, new FacesMessage("Нерадни дани успешно додати"));
            stmt.close();con.close();
            return;
        }catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ex) {}
            }
            context.addMessage(null, new FacesMessage(e.getLocalizedMessage()));
            return;
        }
    }
    //GETTERS AND SETTERS
    /**
     * @return the dan1
     */
    public String getDan1() {
        return dan1;
    }

    /**
     * @param dan1 the dan1 to set
     */
    public void setDan1(String dan1) {
        this.dan1 = dan1;
    }

    /**
     * @return the mesec1
     */
    public String getMesec1() {
        return mesec1;
    }

    /**
     * @param mesec1 the mesec1 to set
     */
    public void setMesec1(String mesec1) {
        this.mesec1 = mesec1;
    }

    /**
     * @return the godina1
     */
    public String getGodina1() {
        return godina1;
    }

    /**
     * @param godina1 the godina1 to set
     */
    public void setGodina1(String godina1) {
        this.godina1 = godina1;
    }

    /**
     * @return the dan2
     */
    public String getDan2() {
        return dan2;
    }

    /**
     * @param dan2 the dan2 to set
     */
    public void setDan2(String dan2) {
        this.dan2 = dan2;
    }

    /**
     * @return the mesec2
     */
    public String getMesec2() {
        return mesec2;
    }

    /**
     * @param mesec2 the mesec2 to set
     */
    public void setMesec2(String mesec2) {
        this.mesec2 = mesec2;
    }

    /**
     * @return the godina2
     */
    public String getGodina2() {
        return godina2;
    }

    /**
     * @param godina2 the godina2 to set
     */
    public void setGodina2(String godina2) {
        this.godina2 = godina2;
    }

    /**
     * @return the razlog
     */
    public String getRazlog() {
        return razlog;
    }

    /**
     * @param razlog the razlog to set
     */
    public void setRazlog(String razlog) {
        this.razlog = razlog;
    }

    /**
     * @return the daniItems
     */
    public ArrayList<SelectItem> getDaniItems() {
        return daniItems;
    }

    /**
     * @param daniItems the daniItems to set
     */
    public void setDaniItems(ArrayList<SelectItem> daniItems) {
        this.daniItems = daniItems;
    }

    /**
     * @return the meseciItems
     */
    public ArrayList<SelectItem> getMeseciItems() {
        return meseciItems;
    }

    /**
     * @param meseciItems the meseciItems to set
     */
    public void setMeseciItems(ArrayList<SelectItem> meseciItems) {
        this.meseciItems = meseciItems;
    }

    /**
     * @return the godineItems
     */
    public ArrayList<SelectItem> getGodineItems() {
        return godineItems;
    }

    /**
     * @param godineItems the godineItems to set
     */
    public void setGodineItems(ArrayList<SelectItem> godineItems) {
        this.godineItems = godineItems;
    }

    /**
     * @return the sala
     */
    public String getSala() {
        return sala;
    }

    /**
     * @param sala the sala to set
     */
    public void setSala(String sala) {
        this.sala = sala;
    }

}
